package ufcg.metci.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Projeto para disciplina de Metodologia Científica - UFCG - 2012.1
 * 
 * @author Ana Clara Lacerda
 * @author Rodolfo Marinho
 *
 * Classe oferece os serviços de arquivo
 */
public class FileControl {
	
	public static String read(String fileName) throws IOException,
			FileNotFoundException {
		int i;
		StringBuffer content = new StringBuffer();
		FileInputStream in = new FileInputStream(fileName);
		while ((i = in.read()) > -1) {
			content.append((char) i);
		}
		in.close();
		return content.toString();
	}
	
	public static void saveCompressedFile(String compressedContent, String inputFile, String outputFile) {
		FileOutputStream fl = null;
        try {
        	fl = new FileOutputStream(outputFile+".huf");
            
        	// Coloca a extensao no arquivo
            String extensionName = recoverCompressedExtension(inputFile);
            byte[] extension = extensionName.getBytes();
            fl.write(extension.length + 48);
            fl.write(extension);

            byte[] dictBytes;
            String dict = "";
            
            String phrase;
            int intByte;
            
            byte[] bZeros;
            String sZeros;
            int zeros = 0;

            for (int i = 0; i < compressedContent.length(); i++) {
                String word = compressedContent.substring(i, i + 6);
                if (word.equals("-D-AFW")) {
                    dict = compressedContent.substring(i, compressedContent.length());
                    compressedContent = compressedContent.substring(0, i);
                    break;
                }
            }
            for (int i = 0; i < compressedContent.length(); i++) {
                if (i % 8 == 0) {
                    if (i + 8 <= compressedContent.length()) {
                        phrase = compressedContent.substring(i, i + 8);
                        i = i + 7;
                    } else {
                        phrase = compressedContent.substring(i, compressedContent.length());
                        while (phrase.length() < 8) {
                            phrase = phrase + "0";
                            zeros++;
                        }
                    }
                    intByte = Integer.parseInt(phrase, 2);
                    fl.write(intByte);
                }
            }
            sZeros = zeros + "";
            bZeros = sZeros.getBytes();
            fl.write(bZeros);
            dictBytes = dict.getBytes("ISO-8859-1");
            fl.write(dictBytes);
            fl.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
	
	public static String recoverCompressedExtension(String input) {
		int extensionSize = input.lastIndexOf(".");
		String extension = input.substring(extensionSize + 1, input.length());
		return extension;
	}

    public static void saveDecompressFile(String content, String outputFile, String extension) {
    	FileOutputStream fl = null;
        try {
        	fl = new FileOutputStream(outputFile + "." + extension);
            byte[] bFrase = content.getBytes();
            fl.write(bFrase);
            fl.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
